package com.fiberhome.ztelauncher;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.widget.Toast;

import com.fiberhome.zte_launcher.R;
import com.fiberhome.ztelauncher.core.MainActivity;
import com.fiberhome.ztelauncher.resource.GetDomainCallBack;
import com.fiberhome.ztelauncher.resource.ResourceCallBack;
import com.fiberhome.ztelauncher.resource.ResourceManager;
import com.fiberhome.ztelauncher.xml.bean.Launcher;

import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;

/**
 * Created by Deed on 2018/4/9.
 */

public class LaunchActivity extends Activity {
    private Launcher launcher;
    private boolean isDestroyed;
    @Override
    protected void onCreate( Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);
        ResourceManager.getResourceManager().getLocationUrl(new GetDomainCallBack() {
			
			@Override
			public void onOk() {
				// TODO Auto-generated method stub
				startGetLauncher();
			}
			
			@Override
			public void onFail() {
				// TODO Auto-generated method stub
				Toast.makeText(LaunchActivity.this.getApplicationContext(),"获取LocationXmlUrl失败",
						Toast.LENGTH_SHORT).show();
				finish();
			}
		});
        
    }
    
    private void startGetLauncher() {
    	ResourceManager.getResourceManager().checkLauncher(new Observer<Launcher>() {
            @Override
            public void onSubscribe(@NonNull Disposable disposable) {

            }

            @Override
            public void onNext(@NonNull Launcher launcher) {
                LaunchActivity.this.launcher = launcher;
                ResourceManager.getResourceManager().initResource(launcher, new ResourceCallBack() {
                    @Override
                    public void onResourceReady() {
                        //resource ready , 进入主页
                        if (!isDestroyed) {
                            startActivity(new Intent(LaunchActivity.this, MainActivity.class));
                            finish();
                        }
                    }

                    @Override
                    public void onResourceFailed() {
                        //resource failed
                        Toast.makeText(LaunchActivity.this.getApplicationContext(),"获取资源文件失败，请重试",Toast.LENGTH_SHORT).show();
                        finish();
                    }
                });
            }

            @Override
            public void onError(@NonNull Throwable throwable) {
                Toast.makeText(LaunchActivity.this.getApplicationContext(),"获取资源文件失败,请检查网络环境",Toast.LENGTH_SHORT).show();
                ResourceManager.getResourceManager().getLauncher(new Observer<Launcher>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable disposable) {

                    }

                    @Override
                    public void onNext(@NonNull Launcher launcher) {
                        //进入主页把
                        if (!isDestroyed) {
                            startActivity(new Intent(LaunchActivity.this, MainActivity.class));
                            finish();
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable throwable) {
                        finish();
                    }

                    @Override
                    public void onComplete() {

                    }
                });
            }

            @Override
            public void onComplete() {

            }
        });
	}

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            finish();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isDestroyed = true;
        if (launcher != null) {
            ResourceManager.getResourceManager().release(launcher);
        }
    }
}
